<?php  
class Model_Wish extends Model{  

   public function add($userid,$id){

      $query=DB::select('wishid')->from('wish')

      ->where('userid', '=',  $userid)

      ->and_where('postid','=',$id)

      ->limit(1)

      ->execute();

      if($query->count()==0){

        try {

            Database::instance()->begin();

            list($insert_id, $total_rows)= DB::insert('wish')

            ->columns(array('postid','userid','createdon'))

            ->values(array($id,$userid,date("Y-m-d h:i:sa")))

            ->execute();

            DB::update('post')->set(array('wish'=>DB::expr('wish+1')))->where('postid','=',$id)->execute();

            Database::instance()->commit();

            return array("code"=>"0x00000");

        } 
        catch (Exception $e) {

          Database::instance()->rollback();

          return array("code"=>"0x0000x");

        }

      }
      else{

        try {

          Database::instance()->begin();

          DB::delete('wish')->where('postid','=',$id)->and_where("userid","=",$userid)->execute();

          DB::update('post')->set(array('wish'=> DB::expr('wish-1')))->where('postid','=',$id)->and_where("wish",">","0")->execute();

          Database::instance()->commit();

          return array("code"=>"0x00003");

        } 
        catch (Exception $e) {

          Database::instance()->rollback();

          return array("code"=>"0x0000x");

        }

      }
     
   }
  
   public function remove($userid,$id){

      DB::delete('wish')->where('wishid', '=' ,$id)->and_where("userid","=",$userid)->execute();

      return array("code"=>"0x00000");

   }

  public function getInfoByUser($userid,$offset,$pageSize){

       $data=DB::select(array('wishid','id'),'post.postid','post.images','post.title','post.description','post.view','post.reach','post.review','post.wish','post.praiseup')
        
        ->from('wish')

        ->join( 'post' , 'LEFT' )->on( 'post.postid' , '=' , 'wish.postid')
        
        ->where('wish.isdeleted','=','0')

        ->and_where("wish.userid","=",$userid)

        ->order_by('wish.createdon','desc')
        
        ->limit($pageSize)
        
        ->offset($offset)
        
        ->execute()->as_array();

        return $data;

  }

  public function getNextInfoByUser($userid,$lastid,$pageSize){

       $data=DB::select(array('wishid','id'),'post.postid','post.images','post.title','post.description','post.view','post.reach','post.review','post.wish','post.praiseup')
        
        ->from('wish')

        ->join( 'post' , 'LEFT' )->on( 'post.postid' , '=' , 'wish.postid')
        
        ->where('wish.isdeleted','=','0')

        ->and_where("wish.userid","=",$userid)

        ->and_where("wishid",'<',$lastid)

        ->order_by('wish.createdon','desc')
        
        ->limit($pageSize)
        
        ->execute()->as_array();

        return $data;

  }

  public function getItemsByUser($userid){

     $total= DB::select(array(DB::expr('count(1)'), 'total'))

         ->from('wish')

        ->where('isdeleted','=','0')

        ->and_where("userid","=",$userid)

        ->execute()

        ->get("total");

        return $total;
  }
}
